package com.zking.dao.mapper;

import com.zking.dao.domain.Patient;
import com.zking.dao.domain.Register;
import com.zking.dao.domain.RegisterMonth;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Tanglei
 * @date 2020/8/21 - 16:08
 */
@Repository
public interface RegisterMapper {

    //查询所有
    @Select("SELECT reg.id , " +
            "p.name,doc.dname,dept.dept_name,reg.create_time," +
            "reg.state FROM register reg,doctor doc," +
            "patient p,department dept WHERE reg.d_id = doc.id " +
            "AND reg.depet_id = dept.id " +
            "AND reg.p_id = p.id")
    List<Register> queryAllRegister();
    @Select("SELECT reg.id , " +
            "p.name,doc.dname,dept.dept_name,reg.create_time," +
            "reg.state FROM register reg,doctor doc," +
            "patient p,department dept WHERE reg.d_id = doc.id " +
            "AND reg.depet_id = dept.id " +
            "AND reg.p_id = p.id " +
            "and reg.p_id =#{id}")
    List<Register> queryAllRegisterByPid(Patient patient);


    @Select("SELECT reg.id , " +
            "p.name,doc.dname,dept.dept_name,reg.create_time," +
            "reg.state FROM register reg,doctor doc," +
            "patient p,department dept WHERE reg.d_id = doc.id " +
            "AND reg.depet_id = dept.id " +
            "AND reg.p_id = p.id"+
            "AND reg.p_id = #{id}"
            )
    List<Register> queryRegisterByID(Patient patient);
    //按月查询注册患者数量
    @Select("SELECT DATE_FORMAT(register.create_time,'%Y-%m') as month,COUNT(*) as nums \n" +
            "FROM register\n" +
            "GROUP BY DATE_FORMAT(register.create_time,'%y%m')\n" +
            "ORDER BY month asc")
    List<RegisterMonth> registerNumsMonthly();

    @Update("UPDATE register SET register.state = '已就诊' WHERE register.id = #{id}")
    void UpdateRegState(int id);
}
